package com.app.web.collection;

import com.app.utils.base.http.ResultUtil;
import com.app.utils.base.j2se.CollectionFileUtil;
import com.app.utils.base.j2se.Common;
import com.app.utils.base.j2se.Convert;
import com.app.utils.base.j2se.JsonUtil;
import com.app.web.goods.category.Category;
import com.app.web.goods.goods.GoodsManager;
import com.app.web.goods.goods.GoodsService;
import com.app.web.merchant.Merchant;
import com.app.web.merchant.MerchantManager;
import com.app.web.merchant.MerchantService;
import com.app.web.user.info.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: tbyy-app->CollectionController
 * @Auther: 王伟
 * @Date: 2019/12/18 16:56
 * @Description: 我的收藏
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserInfoService userInfoService;
    /**
     * 我的收藏信息
     * @param uid       用户id
     * type 1 商家 3 产品
     * @return
     */
    @RequestMapping("/getCollection")
    public String getCollection(@RequestAttribute int uid){
//        List<Map<String,Object>> list = null;
        Map<String,Object> obj = new HashMap<>();
        for(int type=1;type<=3;type++){
            List<Integer> l = getObjId(CollectionFileUtil.getCollection(uid,type));
            obj.put("key_"+type,l);
        }
        return ResultUtil.success(obj);
    }

    /**
     * 添加收藏
     * @param uid
     * @param type
     * @param obj_id
     * @return
     */
    @RequestMapping("/add")
    public String add(@RequestAttribute int uid,int type,int obj_id){
        int i=0;
        try {
            List<Map<String,Object>> list = CollectionFileUtil.add(uid,obj_id,type);
            if(Common.isEmpty(list)){
                i=-1;
            }
        }catch (Exception e){
            i=-1;
            e.printStackTrace();
        }
        if(i==0){
            return ResultUtil.success(0,"收藏成功");
        }else{
            return ResultUtil.error("-1","收藏失败");
        }

    }

    /**
     * 删除收藏
     * @param uid
     * @param type
     * @param obj_id
     * @return
     */
    @RequestMapping("/del")
    public String del(@RequestAttribute int uid,int type,int obj_id){
        int i=0;
        try {
            List<Map<String,Object>> list = CollectionFileUtil.del(uid,obj_id,type);
            if(Common.isEmpty(list)){
                i=-1;
            }
        }catch (Exception e){
            i=-1;
            e.printStackTrace();
        }
        if(i==0){
            return ResultUtil.success(0,"删除收藏成功");
        }else{
            return ResultUtil.error("-1","删除收藏失败");
        }

    }

    /**
     * 我的收藏列表信息
     * @param uid       用户id
     * @param type    1 店铺 3 商品
     * @return
     */
    @RequestMapping("/getMyCollectionList")
    public String getCollectionList(@RequestAttribute int uid,int type){
        List<Map<String,Object>> list = CollectionFileUtil.getCollection(uid,type);
        if(Common.isEmpty(list)){
            return ResultUtil.error(555,"暂无数据");
        }
        List<Map<String,Object>> list1 = new ArrayList<>();
        List<Integer> l = new ArrayList<>();
        for (Map<String,Object> obj:list) {
            int obj_id_c = Convert.objToInt(obj.get("obj_id"));
            l.add(obj_id_c);
        }
        Collections.reverse(l);
        if(type==1){
            Map<Integer, Merchant> objMap = merchantService.merchantCollectionList(l);
            for(int i : l){
                if(objMap.containsKey(i)){
                    list1.add(JsonUtil.beanToMap(objMap.get(i)));
                }
            }
            return ResultUtil.success(list1);
        }
        if(type==3){
            List<Map<String,Object>> objList = goodsService.goodsCollectionList(l);
            List<Map<String,Object>> data = objList.stream().map(GoodsManager::toMap).collect(Collectors.toList())
                .stream().map(x->{
                Map<String,Object> merchant  = MerchantManager.getMerchantById(Convert.objToInt(x.get("merchantId")));
                x.put("merchantName", merchant.get("name"));
                x.put("spot", merchant.get("spot"));
                x.put("catsStr",doctorCatsStr(x.get("id"),3));
                return x;
            }).collect(Collectors.toList());
            for(int i : l){
                for(Map<String,Object> obj : data){
                    int id = Convert.objToInt(obj.get("id"));
                    if(i==id){
                        list1.add(obj);
                        break;
                    }
                }
            }
            return ResultUtil.success(list1);
        }
        return ResultUtil.success();
    }
    /* 根据医生ID 获取并组装医生特长项目类型 */
    List<String> doctorCatsStr(Object doctorId,int count){
        return goodsService.goodsCategoryByType(Convert.objToInt(doctorId), 2,count).stream().map(Category::getName).collect(Collectors.toList());
    }
    List<Integer> getObjId(List<Map<String,Object>> data){
        List<Integer> l = new ArrayList<>();
        if(Common.isEmpty(data)){
            return l;
        }
        for (Map<String,Object> obj:data) {
            int obj_id_c = Convert.objToInt(obj.get("obj_id"));
            l.add(obj_id_c);
        }
        return l;
    }

}
